使用Value属性设置Select的默认值
Set Default Value for Select by using value attribute
我有网页动态创建的选择(下拉),网页创建的shell CGI脚本:
while read line; do
textBoxValue=`printf "$line" | cut -d' ' -f1`
comboBoxValue=`printf "$line" | cut -d' ' -f2`
echo "
<div id="divId$start_id">
<input type="text" name="newTexdtBox$start_id" id="newTextBox" value='$textBoxValue' />
<select name="newComboBox$start_id" id="newComboBox" value="$comboBoxValue">
<option disabled>pls select</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="9">all</option>
</select>
<a href='javascript:void(0)' onclick='return removeThisElement($start_id)'>Remove This</a>
</div>
<a href='javascript:void(0)' onclick='return getSelects()'>getSelects</a>
"
start_id=$((start_id+1))
count=$((count + 1))
done < my_conf
每个Select对象都有自己的属性值=$comboBoxValue。我想在页面加载时将此值属性设置为选中。建议的解决方案是(http://snipplr.com/view/67752/set-default-value-for-select-dropdown-lists-using-value-attribute/),但它似乎不工作,因为this.getAttribute("值")不返回任何东西。
如何设置默认值为选择(下拉列表)使用值属性?
try
<select name="newComboBox$start_id" id="newComboBox" value="$comboBoxValue">
<option value="" selected='selected'>pls select</option>
<option value="1">1</option>
如果你想用jQuery解决
$(function(){
$('select[value]').each(function(){
$('option[value="' + $(this).attr('value') + '"]', this).prop('selected', true);
});
});
演示:小提琴但最佳的解决方案将是解决它使用html标记本身。(我不知道cgi脚本),但像
<select name="newComboBox$start_id" id="newComboBox">
<option disabled>pls select</option>
<option #if ($comboBoxValue == 1) { selected="selected"} value="1">1</option>
<option #if ($comboBoxValue == 2) { selected="selected"} value="2">2</option>
<option #if ($comboBoxValue == 3) { selected="selected"} value="3">3</option>
....
</select>
解决方案http://snipplr.com/view/67752/set-default-value-for-select-dropdown-lists-using-value-attribute/工作正常,我只是忘记了从Shell脚本(getAttribute('"value'"))渲染函数时的符号。
这个可以正常工作(当从CGI Shell脚本渲染时):
'$(document).ready(function() {
'$('select[value]').each(function(){
'$(this).val(this.getAttribute('"value'"));
});
});
相关文章:
- 我的shareService在angular 2中发送值工作正常,但当我渲染我的另一个组件时,会获得默认值
- Angularjs-设置不带ng选项的select的默认值
- JS:如果select是默认值,则禁用复选框
- angularjs-lib中的一些prbm默认值在select if(value和name)相同时不起作用
- AngularJS-多重选择,动态设置默认值select
- 当通过AJAX绑定选项时,是否可以将select元素的默认值传递给Knockout
- Angular Js:您可以将select的默认值与使用'track by'在ng选项中
- 通过单击SELECT选项,将文本INPUT元素的VALUE动态设置为默认值
- Select2.js设置默认值为带有输入数组的select box
- AngularJS ui-select下拉式默认值
- 使用AngularJS的双向数据绑定设置SELECT的默认值
- 如何使用angularjs-json数据设置select 2下拉列表的默认值
- 使用ng-select循环遍历数组对象的默认值
- 在Angular 2中,将默认值设置为select option
- AngularJS - html select的数据绑定默认值
- 不能在AngularJS中使用ng-model和ng-repeat或ng-select在下拉菜单中选择默认值
- 如何设置select选项的默认值
- 不能为ng-model的select设置默认值
- 使用Value属性设置Select的默认值
- ember-cli如何从表单select设置默认值