Home >Web Front-end >JS Tutorial >Realize the linkage effect between input box and drop-down box

Realize the linkage effect between input box and drop-down box

php中世界最好的语言
php中世界最好的语言Original
2018-03-15 09:55:513667browse

This time I will bring you the linkage effect between the input box and the drop-down box, and what are the precautions to achieve the linkage effect between the input box and the drop-down box. The following is a practical case, let's take a look.

As shown in the picture:

##html code

<tr>
 <th scope="row">奖励类型:</th>
 <td><input value="" type="text" name="item" id="reward" style="width: 60px;height: 20px;" placeholder="输入关键字" />
  <select name="reward_id" id="reward_id"> 
   <option value="">---请选择---</option>
   {foreach $reward as $value}
   <option value="{$value[&#39;material_id&#39;]}">{$value['material_name']}</option>
   {/foreach}
  </select>
 </td>
</tr>

JQuery Code

<script type="text/javascript">
 $('#reward').bind('input propertychange', function() {reward();});
 function reward()
 {
  var search = $("#reward").val();
  $.ajax({ 
   type:"get", 
   url:"/mall/config_commodity_info/search_commodity_info", 
   data:{search:search}, 
   success:function(select){ 
     var reward_id = $("#reward_id"); 
     if (select) {
     $("option",reward_id).remove();
     var obj = JSON.parse(select);
     for (var key in obj) {
      var option = "<option value=&#39;"+key+"&#39;>"+obj[key]+"</option>"; 
      reward_id.append(option); 
     }
     } 
   } 
 });
 } 
</script>

PHP Code

public function add_alms()
{
 $reward = $this->materials->selReward();
 $this->assign('reward',$reward);
 return $this->fetch();
}
public function do_add_alms()
{
 $data = Request::instance()->param();
 $this->alms->addAlms($data);
}
I believe you have mastered the method after reading the case in this article, more Please pay attention to other related articles on the php Chinese website!

Recommended reading:

How to achieve dynamic acquisition of Jquery drop-down box data


Ajax and Jquery are combined with the database to implement it Secondary linkage of drop-down box


jQuery implements recursive infinite layer function


jquery obtains the value and text of the select tag

The above is the detailed content of Realize the linkage effect between input box and drop-down box. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn