Get desired value of selectbox through jquery

蹲街弑〆低调 提交于 2019-12-13 09:05:49

问题


Here i have three dropdowns with differents values and the code looks like as follows.

To get the value of the corresponding dropdown using jquery i had this but it always gets same value please have a look

$(".one").hide();
$(".two").hide();
$(".three").show();
var  name_type=$("#abc_type_1").attr('name','type');
$("#abc_type_2").removeAttr('name');
$("#abc_type_3").removeAttr('name');
$("input[name=select_type]:radio").change(function () {
    if($(this).val()=='1')
    {
        $(".one").show();
        $(".two").hide();
        $(".three").hide();

        var  name_type=$("#abc_type_1").attr('name','type');
        $("#abc_type_2").removeAttr('name');
        $("#abc_type_3").removeAttr('name');
    }
    else if($(this).val()=='2')
    {
        $(".one").hide();
        $(".two").show();
        $(".three").hide();

         var  name_type=$("#abc_type_2").attr('name','type');
        $("#abc_type_1").removeAttr('name');
        $("#abc_type_3").removeAttr('name');
    }
    else if($(this).val()=='3')
    {
        $(".one").hide();
        $(".two").hide();
        $(".three").show();

        var  name_type=$("#abc_type_3").attr('name','type');
        $("#abc_type_1").removeAttr('name');
        $("#abc_type_2").removeAttr('name');
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="left_1">
    <input class="magic-radio" type="radio"  name="select_type" id="1" value="1">
    <label for="1">1</label>
    <input class="magic-radio" type="radio"  name="select_type" id="2" value="2">
    <label for="2">2</label>
    <input class="magic-radio" type="radio"  name="select_type" id="3" checked value="3">
    <label for="3">3</label>
</div>
<div class="left_2 one">
    <select name="type" id="abc_type_1" class="form-control abc_type">
        <option value="A">LSK-A</option>
        <option value="B">LSK-B</option>
        <option value="C">LSK-C</option>
    </select>
</div>
<div class="left_2 two">
    <select name="type" id="abc_type_2" class="form-control abc_type">
        <option value="AB">LSK-AB</option>
        <option value="AC">LSK-AC</option>
        <option value="BC">LSK-BC</option>
    </select>
</div>
<div class="left_2 three">
    <select name="type" id="abc_type_3" class="form-control abc_type">
        <option value="super">LSK-SUPER</option>
        <option value="box">BOX-K</option>
    </select>
</div>

Here i want to get the value of the selected select box but i can not get the correct value, please help me to solve.

var form_data={      
                    agent_name: $('#agent_name').val(),
                    number: $('#number').val(),
                    number_from: $('#number_from').val(),
                    number_to: $('#number_to').val(),
                    quantity: $('#quantity').val(),
                    amount: $('#amount').val(),
                    date: $('#date').val(),
                    commision: $('#commision').val(),
                    profit: $('#profit').val(),
                    agent_amount: $('#agent_amount').val(),
                    user_id: $('#user_id').val(),
                    type: name_type.val(),
                  }

回答1:


Problem with script is that you are not handling radio buttons and dropdown while extracting values for posting to server.

JS

var form_data = {
      agent_name: $('#agent_name').val(),
      number: $('#number').val(),
      number_from: $('#number_from').val(),
      number_to: $('#number_to').val(),
      quantity: $('#quantity').val(),
      amount: $('#amount').val(),
      date: $('#date').val(),
      commision: $('#commision').val(),
      profit: $('#profit').val(),
      agent_amount: $('#agent_amount').val(),
      user_id: $('#user_id').val(),
      type: $("#abc_type_"+$("input[name=select_type]:checked").val()).val()
   };

Just replace your form_data with above script. If not works let me know.

This part getting checked value from the radio button.

$("input[name=select_type]:checked").val()

response is like this. 1 or 2 or 3.

$("#abc_type_"+$("input[name=select_type]:checked").val()).val()

Now jquery will get value by targeting the ID. eg #abc_type_1, #abc_type_2, #abc_type_3




回答2:


You add a default value to the inputs and add function that runs when selects change like this Code:

$(".one").hide();
$(".two").hide();
$(".three").show();
var name_type = $("#abc_type_1").attr('name', 'type');
$("#abc_type_2").removeAttr('name');
$("#abc_type_3").removeAttr('name');
$("input[name=select_type]:radio").change(function() {
  if ($(this).val() == '1') {
    $(".one").show();
    $(".two").hide();
    $(".three").hide();

    var name_type = $("#abc_type_1").attr('name', 'type');
    $("#abc_type_2").removeAttr('name');
    $("#abc_type_3").removeAttr('name');

  } else if ($(this).val() == '2') {
    $(".one").hide();
    $(".two").show();
    $(".three").hide();

    var name_type = $("#abc_type_2").attr('name', 'type');
    $("#abc_type_1").removeAttr('name');
    $("#abc_type_3").removeAttr('name');

  } else if ($(this).val() == '3') {
    $(".one").hide();
    $(".two").hide();
    $(".three").show();

    var name_type = $("#abc_type_3").attr('name', 'type');
    $("#abc_type_1").removeAttr('name');
    $("#abc_type_2").removeAttr('name');
  }
});

$("select").change(function(e){
  alert(e.target.value);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="left_1">
  <input class="magic-radio" type="radio" name="select_type" id="1" value="1">
  <label for="1">1</label>
  <input class="magic-radio" type="radio" name="select_type" id="2" value="2">
  <label for="2">2</label>
  <input class="magic-radio" type="radio" name="select_type" id="3" checked value="3">
  <label for="3">3</label>
</div>
<div class="left_2 one">
  <select name="type" id="abc_type_1" class="form-control abc_type">
            <option value="">Select value</option>
            <option value="A">LSK-A</option>
            <option value="B">LSK-B</option>
            <option value="C">LSK-C</option>
        </select>
</div>
<div class="left_2 two">
  <select name="type" id="abc_type_2" class="form-control abc_type">
              <option value="">Select value</option>
            <option value="AB">LSK-AB</option>
            <option value="AC">LSK-AC</option>
            <option value="BC">LSK-BC</option>
        </select>
</div>
<div class="left_2 three">
  <select name="type" id="abc_type_3" class="form-control abc_type">
              <option value="">Select value</option>
            <option value="super">LSK-SUPER</option>
            <option value="box">BOX-K</option>
        </select>
</div>



回答3:


This is how you can get the current Select Box value

Simplest Example

HTML

<select class="select1_class_name" id="select1" data-userid="1">
<option value="1">1</option>
</select>

Jquery

$('#select1').change(function() {
var id = $(this).data('userid');
console.log(id); //you will get on change of selected dropdown
});


来源:https://stackoverflow.com/questions/48866731/get-desired-value-of-selectbox-through-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!