开发者

get value from select box to text box

<head>
  <style>
  p { color:red; margin:4px; }
  b { color:blue; }
  </style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js" charset="utf-8"></script>
</head>
<body>
  <p><input type="text" name="keyword" /></p>
  <select id="multiple" multiple="multiple">
    <option selected="selected">Multiple</option>
    <option>Multiple2</option>

    <option selected="selected">Multiple3</option>
  </select>
<script>
    function displayVals() {
      var multipleValues = $("#multiple").val() || [];
      $("p").html(开发者_开发百科"<b>Multiple:</b> " + 
                  multipleValues.join(", "));
    }

    $("select").change(displayVals);
    displayVals();

</script>

Here I want to hold value in a text box instead of "p". How to do it?? Here is my code


function displayVals() {
      var multipleValues = $("#multiple").val() || [];
      var html = $("p").html();
      $("p").html("<b>Multiple:</b>" + html);

        $("input[name='keyword']").val( multipleValues.join(", "));
    }

    $("select").change(displayVals);
    displayVals();


If you work with input you should use jQuery.val() (http://api.jquery.com/val/). Something like this $("p").val("Multiple: " + multipleValues.join(", ")); But no tag inside.


<input type="text" value="foooooooooo" />
<select id="multiple" multiple="multiple">
<option selected="selected">Multiple</option>
<option>Multiple2</option>
<option selected="selected">Multiple3</option>
</select>

//////////////////////

 function displayVals() {

  var multipleValues = $("#multiple").val() || [];
    var text = "Multiple: " + multipleValues.join(", ");
  $("input").val(text);
 }

$("select").change(displayVals);
displayVals();


How about this?

Javascript

function displayVals() {
  var multipleValues = $("#multiple").val() || [];
  $("#result").val("<b>Multiple:</b> " +
              multipleValues.join(", "));
}

$("select").change(displayVals);
displayVals();

HTML

<input type="text" id="result" ></input>
<select id="multiple" multiple="multiple">
<option selected="selected">Multiple</option>
<option>Multiple2</option>
<option selected="selected">Multiple3</option>
</select>`
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜