[英]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. 我更改了示例代码以反映这一点。
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>
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.