gpt4 book ai didi

javascript - 在 jQuery 中内联 if else 语句

转载 作者:搜寻专家 更新时间:2023-11-01 04:44:23 25 4
gpt4 key购买 nike

我有一个由两个不同按钮执行的 jQuery 函数。

$("#btnSearch, #btnDirectorSearch").click(function () {

此函数构建的部分 html 取决于点击了哪个按钮。我正在使用数据属性来存储我的变量,如下所示:

var div = $(this).data("str");

我正在构建的 html 字符串取决于变量“div”的值。有没有办法在 jQuery 中执行内联 if/else 语句?

if div = "choice1" {
html += '<tr data-str = "str1" data-dataItem = "dataItem1" data-result-title = "' + name + '" data-result-id="' + sel + '">';

} else {
html += '<tr data-str = "str2" data-dataItem = "dataItem2" data-result-title = "' + name + '" data-result-id="' + sel + '">';
}

这看起来很麻烦,我希望有更好的 jQuery 方法来做到这一点。

谢谢!

最佳答案

语法错误

if div = "choice1" 

应该是

if (div == "choice1")

无论如何,您正在寻找的模式是:

div == "choice1" ? <code for true> : <code for false>

关于javascript - 在 jQuery 中内联 if else 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15683352/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com