简体   繁体   English

使用值属性为“选择”设置默认值

[英]Set Default Value for Select by using value attribute

I have web page with dynamically created Selects (dropdown), the web page created by shell CGI script: 我有一个带有动态创建的Selects(下拉列表)的网页,它是由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

Each Select object has his own attribute value=$comboBoxValue. 每个Select对象都有自己的属性value = $ comboBoxValue。 I would like to set this value attribute as selected when page loaded. 我想将此值属性设置为在页面加载时选择的值。 Suggested solution is ( http://snipplr.com/view/67752/set-default-value-for-select-dropdown-lists-using-value-attribute/ ) but it seems dont work because this.getAttribute("value") doesnot return anything. 建议的解决方案是( http://snipplr.com/view/67752/set-default-value-for-select-dropdown-lists-using-value-attribute/ ),但似乎不起作用,因为this.getAttribute(“ value” )不返回任何内容。

How I can set Default Value for Select (dropdown list) by using value attribute? 如何通过使用value属性为Select(下拉列表)设置Default Value?

try 尝试

<select name="newComboBox$start_id" id="newComboBox" value="$comboBoxValue">
      <option value="" selected='selected'>pls select</option>
      <option value="1">1</option>

If you want to solve it using jQuery 如果您想使用jQuery解决问题

$(function(){
    $('select[value]').each(function(){
        $('option[value="' + $(this).attr('value') + '"]', this).prop('selected', true);
    });
});

Demo: Fiddle 演示: 小提琴

But the optimal solution will be to solve it using html markup itself. 但是最佳的解决方案是使用html标记本身解决它。 (I don't know cgi scripts), but something like (我不知道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>

The solution http://snipplr.com/view/67752/set-default-value-for-select-dropdown-lists-using-value-attribute/ is working properly, I just forgot \\ symbol when render function from Shell script (getAttribute(\\"value\\")). 解决方案http://snipplr.com/view/67752/set-default-value-for-select-dropdown-lists-using-value-attribute/正常运行,当我从Shell脚本渲染功能时,我只是忘记了\\符号(的getAttribute(\\ “值\\”))。

This works fine (when rendering from CGI Shell script): 正常工作(从CGI Shell脚本渲染时):

\$(document).ready(function() {
  \$('select[value]').each(function(){  
    \$(this).val(this.getAttribute(\"value\"));        
  });                                                 
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM