简体   繁体   中英

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:

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. 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.

How I can set Default Value for Select (dropdown list) by using value attribute?

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

$(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. (I don't know cgi scripts), but something like

<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\\")).

This works fine (when rendering from CGI Shell script):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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