简体   繁体   English

如何使用动态ID在HTML下拉列表中选择文本

[英]How to get selected Text in a HTML drop down using dynamic ID

I am trying to pull values from a JSON string on my web page. 我试图从我的网页上的JSON字符串中提取值。 I have used dynamic ID in my select drop-down. 我在选择下拉列表中使用了动态ID。 Now I want to create an IF loop such that it compares if the selected value in the drop down is equal to "some text" then it should print the result in another field. 现在我想创建一个IF循环,以便比较下拉列表中的选定值是否等于“some text”,然后它应该在另一个字段中打印结果。 Here I want the final value in place of "I WANT VALUE HERE". 在这里,我希望最终的价值取代“我想在这里取值”。 Here is my piece of code. 这是我的一段代码。

<div class="row">
<span class="input-field col wide20">
     <input readonly="readonly" type="text" value="" class="form-control" id="<?php echo $key; ?>_name_Broker" placeholder="">
</span>
     <span class="input-field col wide20">
          <!--input type="text" class="form-control" placeholder=""-->
     <select style="width: 100%;" name="mapped_Broker" class="form-control mappedBroker" data-id="<?php echo $key; ?>" id="<?php echo $key; ?>_select_Broker">
     <option value="">--Select--</option>
         <?php foreach ($my_array_data['BrokerName'] as $data2){
             for ($i = 0; $i < count($data2['Entityname']); $i++) { ?>
             <option value="<?php echo $data2['Senetence']; ?>"><?php echo $data2['Entityname']; ?></option>
         <?php }
        }
        ?>
       </select>
<input style="display:none;" type="text" id="<?php echo $key; ?>_edit_Broker" name="Edited_Sub_Broker">
</span>
<span class="input-field col wide20">
<button type="submit" class="btn btn-dark-grey">Broker</button>
</span>
<span class="input-field col wide10">
<span class="icon-edit" id="span_edit_Broker"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" enable-background="new 0 0 64 64" id="Layer_1" version="1.1" viewBox="0 0 64 64" xml:space="preserve">
<g>
<path d="M55.736 13.636l-4.368-4.362c-0.451-0.451-1.044-0.677-1.636-0.677 -0.592 0-1.184 0.225-1.635 0.676l-3.494 3.484 7.639 7.626 3.494-3.483C56.639 15.998 56.639 14.535 55.736 13.636z"/>
<polygon points="21.922 35.396 29.562 43.023 50.607 22.017 42.967 14.39 "/>
<polygon points="20.273 37.028 18.642 46.28 27.913 44.654 "/>
<path d="M41.393 50.403H12.587V21.597h20.329l5.01-5H10.82c-1.779 0-3.234 1.455-3.234 3.234v32.339c0 1.779 1.455 3.234 3.234 3.234h32.339c1.779 0 3.234-1.455 3.234-3.234V29.049l-5 4.991V50.403z"/>
</g>
</svg> </span>
</span>

<span class="input-field col wide20">
<input readonly="readonly" type="text" class="form-control" placeholder="" 
name="brokersuggestion" value="<?php echo $jsonArrData['brokersuggestion'] ?
>">
</span>

<span class="input-field col wide10">
 <input readonly="readonly" type="text" value="" class="form-control" id="<?php echo $key; ?>_conf_Broker" placeholder="I WANT VALUE HERE"
 </span>
  </div>

EDIT: My JSON looks like this. 编辑:我的JSON看起来像这样。

"BrokerName": [
    {
      "Entityname": "AmWINS",
      "EntityType": "BrokerName",
      "FileType": "EMAIL",
      "OrignalFileName": "FW  American Hospitality Association  Inc  - GL Sub- Eff 7 27.msg",
      "OrignalFile": "E:\\emails\\80 Submission\u0027s Email\\FW  American Hospitality Association  Inc  - GL Sub- Eff 7 27.msg",
      "ExtlFileName": "MSG_AmericanHospitalityAssociation.txt",
      "ExtFilePath": "E:\\80 txt\\AmericanHospitalityAssociationInc-GLSub-Eff727\\MSG_AmericanHospitalityAssociation.txt",
      "Senetence": "o quote . Thank you Jessica Tonkinson Vice President AmWINS",
      "Confidence": 0.1
    },

here's a working fiddle that might help guide you in the correct direction. 这是一个工作小提琴 ,可能有助于指导您正确的方向。

Here it is listening to the change event on the selection and comparing its value to whatever you want. 在这里,它正在监听选择的变化事件,并将其值与您想要的任何值进行比较。

<select class="selection">
   <option value="one">one</option>
   <option value="two">two</option>
   <option value="three">three</option>
 </select>

Note that i added a class to your selection to make sure to only trigger when that specific selection changes its value 请注意,我在您的选择中添加了一个类,以确保仅在特定选择更改其值时触发

$(".selection").change((e)=>{
 if(e.target.value == "two"){
     console.log("tada");
   }else{
     console.log("wrong");
  }
})

you said you needed the code on the page itself, so just wrap it inside a <script> tag and you should be good. 你说你需要页面本身的代码,所以只需将它包装在<script>标签内,你应该很好。

EDIT: i don't want to give you a whole bunch of code, since i think you can figure that out from there, but here's a little bit of help: 编辑:我不想给你一大堆代码,因为我认为你可以从那里找到它,但这里有一些帮助:

 $(".selection").change((e)=>{
    let yourEntityName = e.target.val; // <--- the selection 
 })

this is how you get the string of your chosen entity. 这是你获得所选实体的字符串的方式。 now, this needs to be compared to your json object, to get whatever corresponding string you want, right? 现在,这需要与你的json对象进行比较,以获得你想要的任何相应的字符串,对吧?

well, we need to search over the entries in the BrokerName array to find the correct entry (assuming there are more than one, right?) I will just assume that your array from json is named BrokerNameArr 好吧,我们需要搜索BrokerName数组中的条目以找到正确的条目(假设有多个,对吧?)我将假设你的json数组名为BrokerNameArr

 let correspondingStuffWeNeed = "";
 for( let i = 0; i < BrokerNameArr.Length; i++){
    if(BrokerNameArr[i]["Entityname"] == yourEntityName){
       correspondingStuffWeNeed = BrokerNameArr[i]["whateverYouNeed"];
       break; //since we got what we wanted
    }
 } 

and this stuff above gets packed inside the change event (or inside a method and then called, your choice) and then you have the value you need. 上面的这些东西被包装在change事件中(或者在一个方法中然后被调用,你的选择),然后你就拥有了你需要的价值。

I hope this is what you are looking for. 我希望这就是你要找的东西。 I think your are confused with how you are going to track that dynamic Id in your <select> element. 我认为您对如何在<select>元素中跟踪动态Id感到困惑。 So wrap it inside a div with an ID and refer from that. 所以将它包装在带有ID的div中并从中引用。

 $('#optionContainer > select').change(function(){ //Get the selected item from dropdown var selectedContent = $(this).find('option:selected').html(); //Sample JSON string. I included "{" and "}" to the front and back of the string to make it object. var jsonContent = '{"BrokerName": [{"Entityname":"AmWINS","EntityType": "BrokerName","FileType": "EMAIL","OrignalFileName": "AA","OrignalFile": "BB","ExtlFileName":"CC","ExtFilePath": "DD","Senetence": "EE","Confidence": 0.1}, {"Entityname":"FmWINS","EntityType": "BrokerName","FileType": "EMAIL","OrignalFileName": "AA","OrignalFile": "BB","ExtlFileName":"CC","ExtFilePath": "DD","Senetence": "EE","Confidence": 0.5}]}' ; //Make your JSON string and object var stringContent = JSON.parse(jsonContent) //Compare entity name with selected item for(i=0; i<stringContent.BrokerName.length; i++ ){ if (stringContent.BrokerName[i].Entityname.indexOf(selectedContent) >= 0){ alert("Confidence " + stringContent.BrokerName[i].Confidence) } } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="optionContainer"> <select> <option value="0">AmWINS</option> <option value="1">FmWINS</option> </select> 

PHP : PHP

Read that json array and check that drop down value is there in JSON like below: 读取json数组并检查JSON中的下拉值是否如下所示:

$decode_data = json_decode($json);
foreach($decode_data as $key=>$value){
if($value == $mapped_Broker){
$result = "place holder";
}
}

Then use that variable in placeholder like below: 然后在占位符中使用该变量,如下所示:

<span class="input-field col wide10">
<input readonly="readonly" type="text" name="conf_Broker" value="" class="form-control" id="<?php echo $key; ?>_conf_Broker" placeholder="<?php echo $result;?>"
</span>

Note: This will work if you have the dropdown value when page load 注意:如果您在页面加载时有下拉值,这将有效

Jquery : Jquery

You can use this code even if you have dynamic id. 即使您有动态ID,也可以使用此代码。

$(function(){
var json_value={"0":"aaa","1":"bbb","2":"ccc","3":"ddd"};
var arr = $.map(o, function(el) { return el; })
$('[name=mapped_Broker]').on('change',function(){
if($.inArray($(this).text(), arr){
$('[name=conf_Broker]').attr('placeholder','your_content');
}
});
});

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

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