我有一個<select></select>
現在列表中包含像a, b, and c
現在我想一些選項,當用戶在textbox
a
,點擊按鈕a
應在選擇列表中選擇,從今以後,任何人都可以幫我在JQuery中做到這一點。設置列表中的選項中選擇使用JQuery
回答
假設這是您的select
:
<select>
<option id="a" value="a"/>
<option id="b" value="b"/>
<option id="c" value="c"/>
<option id="d" value="d"/>
</select>
這就是你input
<input type="text" id="textbox" /> <input type="button" id="button"/>
這是你的script
<script>
$('#button').click(new function(){
var option = $("#textbox").val();
$('select #'+option).attr("selected='true'");
});
</script>
這項工作?
對於像下面的標記,
<select id="selectOpt">
<option value="a">a</option>
<option value="b">b</option>
<option value="c">c</option>
</select>
<input type="textbox" id="txtbox1" />
<button id="sch">Select</button>
下面的腳本應該做的伎倆,
$('#sch').on('click', function() { //<-Bind click handler for button using .on
var txtVal = $('#txtbox1').val(); //<-Get the value entered in the textbox
if ($('#selectOpt option[value=' + txtVal + ']').length > 0) {
//^-- Above line checks if the entered value exist in the options list
$('#selectOpt').val(txtVal); //<-Set the value of the select with the entered value
}
});
的Javascript:
$(function(){
$("#someButton").click(function(){
$("#someSelect").val($("#someInput").val());
});
});
HTML:
<select id="someSelect">
<option>a</option>
<option>b</option>
<option>c</option>
</select>
<input id="someInput" type="text" />
<button id="someButton">Go</button>
看到這裏工作的例子:http://jsbin.com/ipevaz/3/edit
您將使用該代碼單擊任意*按鈕後,將* all *選項的值設置爲* all * input元素的第一個值。 – 2012-04-19 20:27:16
我知道這一點。如果他不明白,那麼他可能根本不應該使用jquery。 – 2012-04-19 20:28:33
看問題,你認爲他/她理解jQuery嗎? – 2012-04-19 20:29:07
<select>
<option value="a">a</option>
<option value="b">d</option>
<option value="c">c</option>
</select>
<input type="text" id="letext" maxlength="1"/>
<input type="button" id="select" value="Select" />
$('#select').click(function(){
$('select option[value=' + $('#letext').val() +']').prop('selected','true')
});
http://jsfiddle.net/chepe263/UNmT8/
按鈕功能是找到等於輸入
您可以使用下面的HTML做到這一點的價值選擇。
<input type="text" id="input" />
<input type="button" id="btn" value="Move to Select" />
<select>
<option value="a">a</option>
<option value="b">b</option>
<option value="c">c</option>
</select>
以及這個jQuery代碼片段。
<script type="text/javascript">
$(function() {
$('#btn').click(function() {
$('select option[value="' + $('#input').val() + '"]').prop('selected', true);
});
});
</script>
查看this Fiddle進行工作演示。希望有所幫助! :)
- 1. 使用jQuery選擇的設置選項
- 2. 設置選擇選項使用jQuery
- 3. 爲jquery中的所有選中行設置「選擇」選項?
- 4. 使用jquery在選擇框中設置一個特定選項
- 5. jQuery的設置選擇的選項
- 6. 使用jQuery隱藏選擇列表中的選項
- 7. 的jQuery:嘗試設置選擇選項
- 8. jQuery的設置選擇選項對HTML
- 9. jQuery的:選擇列表項
- 10. 在選擇列表中的jquery選擇
- 11. 如何使用jquery從下拉列表中選擇選項?
- 12. 如何在jQuery中設置選擇選項的選定值?
- 13. 樹枝設置在下拉列表中選擇選項
- 14. Zend Framework 2:在選擇列表中將選項設置爲「selected」
- 15. Javascript從選擇下拉列表中選擇選項,使用href
- 16. jQuery/ajax,設置選擇選項
- 17. 使用URL查詢將選擇列表值設置爲選中
- 18. 設置html使用c選擇選項#
- 19. 的jQuery遍歷選擇和設置選擇的選項
- 20. 當HTML選擇列表項中選擇
- 21. jQuery中的列表選擇
- 22. jQuery中選擇選擇列表
- 23. 關於使用jQuery設置HTML選擇選項的問題
- 24. jQuery選擇列表項
- 25. 如何在PHP表單中設置用戶選擇的選項
- 26. 設置選項列表使用formHTML.append
- 27. 使用jQuery選擇基於標籤從選擇列表中的項目
- 28. 使用jQuery選擇下拉列表中的所有可見選定選項
- 29. HTML選擇列表中設置
- 30. 如何在jQuery中選擇多個選擇列表中的選項?
您應該進行檢查以確保選項存在。雖然它可能沒有區別。 – 2012-04-19 20:26:13
@DavidThomas是的,問題是不完整的..所以我扔了一個粗糙的版本。 – 2012-04-19 20:26:47
我知道;但只要你試圖回答... = b – 2012-04-19 20:28:34