简体   繁体   English

我如何有一个form :: select运行脚本来更改目标,以便每次选择不同的选项时样式都会更改?

[英]How do I have a form::select run a script to change the target so the style changes each time a different option is selected?

The problem is I'm working with a Laravel Form::select() and it's to choose the subpriority. 问题是我正在使用Laravel Form :: select(),它是选择子优先级的。 Each Subpriority has a background color. 每个子优先级都有背景颜色。 So I set a style for each one that checks the id and target so my idea was to have a script that changes the target of the form when the current option in the dropdown is selected. 因此,我为检查ID和目标的每个样式都设置了一种样式,因此我的想法是拥有一个脚本,当在下拉列表中选择当前选项时,该脚本会更改表单的目标。 But I can't seem to get javascript to run when a change occurs or if it does, I can't get it to change the target. 但是我似乎无法使javascript在发生更改时运行,或者如果发生更改,就无法更改目标。

I've tried using jquery, ajax, javascript and creating a similar script to what runs for the priority box but nothing seems to work. 我尝试使用jquery,ajax,javascript并创建与优先级框运行的脚本类似的脚本,但似乎没有任何效果。

<style>
    #sub[target="65"]{
        background-color: #006600;
        color: white;
    }
    #sub[target="66"]{
        background-color: #99ff66;
        color: white;
    }
    #sub[target="67"]{
        background-color: #bbff99;
        color: white;
    }
    #sub[target="68"]{
        background-color: #ffff99;
        color: white;
    }
    #sub[target="69"]{
        background-color: #ffff80;
        color: black;
    }
    #sub[target="70"]{
        background-color: #ffff00;
        color: white;
    }
    #sub[target="71"]{
        background-color: #ff944d;
        color: white;
    }
    #sub[target="72"]{
        background-color: #ff751a;
        color: white;
    }
    #sub[target="73"]{
        background-color: #ff6666;
        color: white;
    }
    #sub[target="74"]{
        background-color: #800000;
        color: white;
    }
</style>

{!! Form::select('subpriority', [65=>'A',66=>'B',67=>'C',68=>'D',69=>'E',70=>'F',71=>'G',72=>'H',73=>'I',74=>'J'], 69, [ 'class' => 'form-control', 'id'=> 'sub', 'target'=>'69' ]); !!}

<script>
    $(#sub).change(function(){
        $(#sub).setAttribute('target', $(#sub).val());
    });
</script>

So I want it to just to change the background color of the select form when I select a new option. 因此,我希望它仅在选择新选项时更改选择表单的背景颜色。 Right now it properly sets it to the style for #sub[target="69"] but either the target isn't changing or the style doesn't change with the target. 现在,它已将其正确设置为#sub [target =“ 69”]的样式,但目标没有改变,或者样式没有随目标改变。 So the dropdown box starts off yellow with the letter E. If I change to A, then the background color should change to green. 因此,下拉框以字母E开头为黄色。如果我更改为A,则背景色应更改为绿色。 So and so forth for the other options. 其他选项等等。

You are using a mixture of jQuery and vanilla JavaScript here. 您在此处混合使用jQuery和香草JavaScript。 setAttribute() is not a jQuery method, but ordinary JavaScript. setAttribute()不是jQuery方法,而是普通的JavaScript。 You also forgot to add single or double quotes around your selectors. 您还忘记了在选择器周围添加单引号或双引号。 In the future, open up the developer console of your browser by pressing F12 and look in the console to see if there are any JavaScript errors. 将来,请按F12打开浏览器的开发人员控制台,然后在控制台中查看是否有JavaScript错误。 That would have quickly shown you what is wrong with the code. 那会很快向您展示代码的问题。

By the way, a native select element has no target attribute defined, so it would be better to use a custom data-target attribute. 顺便说一句,本机select元素没有定义target属性,因此最好使用自定义data-target属性。 I changed my example code to reflect that. 我更改了示例代码以反映这一点。

Here's the fixed code using jQuery: 这是使用jQuery的固定代码:

Using attr() instead of setAttribute() 使用attr()代替setAttribute()

 $('#sub').change(function(){ $('#sub').attr('data-target', $('#sub').val()); }); 
  #sub[data-target="65"]{ background-color: #006600; color: white; } #sub[data-target="66"]{ background-color: #99ff66; color: white; } #sub[data-target="67"]{ background-color: #bbff99; color: white; } #sub[data-target="68"]{ background-color: #ffff99; color: white; } #sub[data-target="69"]{ background-color: #ffff80; color: black; } #sub[data-target="70"]{ background-color: #ffff00; color: white; } #sub[data-target="71"]{ background-color: #ff944d; color: white; } #sub[data-target="72"]{ background-color: #ff751a; color: white; } #sub[data-target="73"]{ background-color: #ff6666; color: white; } #sub[data-target="74"]{ background-color: #800000; color: white; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <form> <select data-target="69" class="form-control" id="sub"> <option value="65">A</option> <option value="66">B</option> <option value="67">C</option> <option value="68">D</option> <option value="69">E</option> <option value="70">F</option> <option value="71">G</option> <option value="72">H</option> <option value="73">I</option> <option value="74">J</option> </select> </form> 

And here's how you could do it with vanilla JavaScript, saving the overhead if you're not using jQuery anyway. 这是使用香草JavaScript的方法,如果无论如何都不使用jQuery,可以节省开销。

 var sub = document.getElementById('sub'); sub.addEventListener('change', function() { this.setAttribute('data-target', this.value); }); 
  #sub[data-target="65"]{ background-color: #006600; color: white; } #sub[data-target="66"]{ background-color: #99ff66; color: white; } #sub[data-target="67"]{ background-color: #bbff99; color: white; } #sub[data-target="68"]{ background-color: #ffff99; color: white; } #sub[data-target="69"]{ background-color: #ffff80; color: black; } #sub[data-target="70"]{ background-color: #ffff00; color: white; } #sub[data-target="71"]{ background-color: #ff944d; color: white; } #sub[data-target="72"]{ background-color: #ff751a; color: white; } #sub[data-target="73"]{ background-color: #ff6666; color: white; } #sub[data-target="74"]{ background-color: #800000; color: white; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <form> <select data-target="69" class="form-control" id="sub"> <option value="65">A</option> <option value="66">B</option> <option value="67">C</option> <option value="68">D</option> <option value="69">E</option> <option value="70">F</option> <option value="71">G</option> <option value="72">H</option> <option value="73">I</option> <option value="74">J</option> </select> </form> 

暂无
暂无

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

相关问题 使用JavaScript,我如何重置表单中的所有选择,以便选择每个选项的第一个选项? - Using JavaScript, how can I reset all the select's in my form so that the first option for each is selected? 如何在已禁用和已选中的选择中设置选项的样式? - How do I style an option in a select that is both disabled and selected? 我如何在Dojo form.select选项中设置选中 - how do i set selected in Dojo form.select option 如何将不同的数据目标分配给不同的选项值,以便显示不同的模型? - How do I assign different data-target to different option values so different models shows up? 如何通过更改 html 表单中的选择选项来更改变量? - How do I change a variable with the change of a select option in an html form? 如何在Rails表单中未预先选择选择选项 - How to Have a Select Option NOT Pre-Selected in a Rails Form 如何获取所有没有使用 jQuery 选择选项的选择元素? - How do I get all select elements that do not have an option selected using jQuery? 当我单击反应中的按钮时,如何定位和更改不同元素的样式 - How do I target and change the style of a different element when I click a button in react 为什么选择选项更改事件不能有这个和 event.target 来获取选定的值? - Why select option change event can't have this and event.target to get selected value? 每次从选择框中选择不同的选项时,都想调用不同的Javascript方法 - Want to call a different Javascript method each time a different option is selected from select box
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM