简体   繁体   English

如何在此Javascript中添加if / else语句?

[英]How can I add an if/else statement to this Javascript?

I've created a table with a dropdown menu which will be used as a comparison chart. 我创建了带有下拉菜单的表格,该表格将用作比较表。 When the user selects a product from the dropdown the cells in the column bellow are populated with the details of the product which are stored in an array. 当用户从下拉列表中选择产品时,将在波纹管列中的单元格中填充存储在数组中的产品详细信息。

So far this is working great but I'm having a problem to add a if/else statement to the script so that instead of just listening to the dropdown of col2 we would also detect a product selection from col3 or col4. 到目前为止,这很好用,但是我在向脚本中添加if / else语句时遇到了问题,因此我们不仅要侦听col2的下拉列表,而且还要检测col3或col4的产品选择。

$(".select2").change(function() {
var jthis = $(this);
var whichCol;
if (jthis.hasClass("col2")) {
    whichCol = "col2";
}

As you can see right now I'm listening to the change in the dropdown "select2" which has the class col2 and then writes to the td's with the ID col2. 正如您现在所看到的,我正在侦听下拉列表“ select2”中的更改,该列表具有类col2,然后写入ID为col2的td。 I can't beat my had around how I can make the script also detect a change to col3 and col4. 我无法抗拒如何使脚本也检测到col3和col4的变化。

The full table 满桌

<h3>Product Comparision Chart Test</h3>
<table class="table table-bordered table-striped">
<thead>
<tr>
    <th class="center"></th>
    <th>
        <select class="col2 select2">
            <option>Prod1</option>
            <option>Prod2</option>
            <option>Prod3</option>
            <option>Prod4</option>
        </select>
    </th>
    <th>
        <select class="col3 select3">
            <option>Prod1</option>
            <option>Prod2</option>
            <option>Prod3</option>
            <option>Prod4</option>
        </select>
    </th>
    <th>
        <select class="col4 select4">
            <option>Prod1</option>
            <option>Prod2</option>
            <option>Prod3</option>
            <option>Prod4</option>
        </select>
    </th>
 </tr>
</thead>
<tbody>
<tr>
    <td><strong>Logo</strong></td>
    <td class="col2 logo"></td>
    <td class="col3 logo"></td>
    <td class="col4 logo"></td>
</tr>
<tr>
    <td><strong>Details 1</strong></td>
    <td class="col2 d1"></td>
    <td class="col3 d1"></td>
    <td class="col4 d1"></td>
</tr>
<tr>
    <td><strong>Details 2</strong></td>
    <td class="col2 d2"></td>
    <td class="col3 d2"></td>
    <td class="col4 d2"></td>
</tr>
  <tr>
    <td><strong>Details 3</strong></td>
    <td class="col2 d3"></td>
    <td class="col3 d3"></td>
    <td class="col4 d3"></td>
  </tr>
  <tr>
    <td><strong>Details 4</strong></td>
    <td class="col2 d4"></td>
    <td class="col3 d4"></td>
    <td class="col4 d4"></td>
  </tr>
 <tr>
    <td><strong>Rating Icons</strong></td>
    <td class="col2 rating"></td>
    <td class="col3 rating"></td>
    <td class="col4 rating"></td>
  </tr>
  <tr>
    <td><strong>Link to website</strong></td>
    <td class="center"><a href="#" target="_blank" class="button-more">link button</a></td>
    <td class="center"><a href="#" target="_blank" class="button-more">Link Button</a></td>
    <td class="center"><a href="#" target="_blank" class="button-more">Link button</a></td>
  </tr>
  </tbody>
</table>

The JS JS

var data = {
"brokers":
    {
    "broker": [
        {
        "name": "Prod1",
        "logo": "P1 Logo",
        "d1": "Specs of this",
        "d2": "Some Details",
        "d3": "More text about this",
        "d4": "Even more details here",
        "rating": "3 stars"
        },
    {
        "name": "Prod2",
        "logo": "P2 Logo",
        "d1": "Specs here",
        "d2": "Details go here",
        "d3": "wow, more text",
        "d4": "Even more text and details",
        "rating": "1 stars"
        },
    {
        "name": "Prod3",
        "logo": "P3 Logo",
        "d1": "Specs and stuff",
        "d2": "Details or some other things",
        "d3": "More details go here wow",
        "d4": "Almost forgot - more here",
        "rating": "5 stars"
        },
    {
        "name": "Prod4",
        "logo": "P4 Logo",
        "d1": "Specs, stuff etc",
        "d2": "Some other things",
        "d3": "What should I say",
        "d4": "details go here wow",
        "rating": "4 stars"
        }
    ]}
};

$(".select2").change(function() {
var jthis = $(this);
var whichCol;
if (jthis.hasClass("col2")) {
    whichCol = "col2";
}
$.each(data.brokers.broker, function(i, v) {
    if (v.name == jthis.val()) {
        $("td." + whichCol + ".name").html(v.name);
        $("td." + whichCol + ".logo").html(v.logo);
        $("td." + whichCol + ".d1").html(v.d1);
        $("td." + whichCol + ".d2").html(v.d2);
        $("td." + whichCol + ".d3").html(v.d3);
        $("td." + whichCol + ".d4").html(v.d4);
        $("td." + whichCol + ".rating").html(v.rating);
        return;
    }
});

});

I would also like to change the jQuery for "logo" to change the src of an image like the products logo for example. 我还想更改jQuery的“徽标”,以更改图像(例如产品徽标)的src。 I've been looking around Stack Overflow and some other sites but can't figure out to solve either one of the two problems myself... yes, I'm not a big java guru but any help is much appreciated. 我一直在寻找Stack Overflow和其他一些站点,但是我自己却想不出解决两个问题中的任何一个...是的,我不是Java专家,但是可以提供任何帮助。

The code is also up on my jsfiddle 代码也在我的jsfiddle上

Please try this: 请尝试以下方法:

Just replace this in your javascript file. 只需将其替换为您的javascript文件即可。

$("select").change(function() {
    var jthis = $(this);
    var whichCol;

       if(jthis.attr("class")=="col2 select2")
       {
           whichCol = "col2";
       }
       else if(jthis.attr("class")=="col3 select3")
       {
           whichCol = "col3";
       }
       else if(jthis.attr("class")=="col4 select4")
       {
           whichCol = "col4";
       }

    $.each(data.brokers.broker, function(i, v) {
        if (v.name == jthis.val()) {
            $("td." + whichCol + ".name").html(v.name);
            $("td." + whichCol + ".logo").html(v.logo);
            $("td." + whichCol + ".d1").html(v.d1);
            $("td." + whichCol + ".d2").html(v.d2);
            $("td." + whichCol + ".d3").html(v.d3);
            $("td." + whichCol + ".d4").html(v.d4);
            $("td." + whichCol + ".rating").html(v.rating);
            return;
        }
    });

});

I'm not sure I understand you intention. 我不确定我是否了解您的意图。 I think you're saying that you can respond to a change event of the select element with the class=select2, but you're unsure of how to achieve the same functionality with the other 2 elements. 我想您是说您可以使用class = select2响应select元素的change事件,但是您不确定如何与其他2个元素实现相同的功能。

If that's the case, just add another onchange handler for each - ie: 如果是这样,只需为每个添加另一个onchange处理程序-即:

$(".select3").change(function() {
    var jthis = $(this);
    var whichCol;
    if (jthis.hasClass("col3")) {
        whichCol = "col3";
    }
    $.each(data.brokers.broker, function(i, v) {
        if (v.name == jthis.val()) {
            $("td." + whichCol + ".name").html(v.name);
            $("td." + whichCol + ".logo").html(v.logo);
            $("td." + whichCol + ".d1").html(v.d1);
            $("td." + whichCol + ".d2").html(v.d2);
            $("td." + whichCol + ".d3").html(v.d3);
            $("td." + whichCol + ".d4").html(v.d4);
            $("td." + whichCol + ".rating").html(v.rating);
            return;
        }
    });

});
​

JsFiddle here: http://jsfiddle.net/enhzflep/ddXYt/7/ 此处的JsFiddle: http : //jsfiddle.net/enhzflep/ddXYt/7/

Think I've solved it myself unless someone can think of a better way of doing this. 认为除非有人能想到一种更好的方法,否则我自己已经解决了。 Ive changed all select elements to "select", did the same for the script and added the if/else statement. 我已经将所有选择元素更改为“选择”,对脚本进行了相同的操作,并添加了if / else语句。

$(".select").change(function() {
var jthis = $(this);
var whichCol;
if (jthis.hasClass("col2")) {
    whichCol = "col2";
} else if
    (jthis.hasClass("col3")) {
    whichCol = "col3";
} else if
(jthis.hasClass("col4")) {
    whichCol = "col4";
}
$.each(data.brokers.broker, function(i, v) {
    if (v.name == jthis.val()) {
        $("td." + whichCol + ".name").html(v.name);
        $("td." + whichCol + ".logo").html(v.logo);
        $("td." + whichCol + ".d1").html(v.d1);
        $("td." + whichCol + ".d2").html(v.d2);
        $("td." + whichCol + ".d3").html(v.d3);
        $("td." + whichCol + ".d4").html(v.d4);
        $("td." + whichCol + ".rating").html(v.rating);
        return;
    }
});

});

Works fine,updated the fiddle 效果很好,更新了小提琴

You can try to do something like: 您可以尝试执行以下操作:

$(".select2").change(function() {
       var jthis = $(this);
       var whichCol = jthis.attr("class").split(" ")[0];
     $.each(data.brokers.broker, function(i, v) {
        if (v.name == jthis.val()) {
            $("td." + whichCol + ".name").html(v.name);
            $("td." + whichCol + ".logo").html(v.logo);
            $("td." + whichCol + ".d1").html(v.d1);
            $("td." + whichCol + ".d2").html(v.d2);
            $("td." + whichCol + ".d3").html(v.d3);
            $("td." + whichCol + ".d4").html(v.d4);
            $("td." + whichCol + ".rating").html(v.rating);
            return;
        }
    });
});​

Here you have the examle jsfiddle 在这里,您有示例jsfiddle

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

相关问题 如何向此 Jquery 添加 else 语句? - How can I add an else statement to this Jquery? 如何向此函数添加 if else 条件语句? - How can I add an if else conditional statement to this function? 如何将“else”语句的值添加到它之前的“if”? - How can I add the the value of an “else” statement to it's previous “if”? 我如何在if或else语句中添加图片而不是文本 - How can i add a picture instead of text in an if or else statement 如何将这种三元语法转换为 JavaScript 中的 if/else 语句? - How can I convert this ternary syntax to an if/else statement in JavaScript? 如何在 JavaScript 中使用三元运算符更改函数中的 if else 语句? - How can I change the if else statement in the function with Ternary operator in JavaScript? 如何将第三个 javascript else if 语句添加到按钮? - How to add a third javascript else if statement to a button? 我可以在内联javascript if语句中省略else吗? - Can I omit the else in an inline javascript if statement? 如何在if ... else if ... else Javascript语句中添加CSS代码 - How to add CSS code within if…else if…else Javascript statement 如何在 JavaScript 中使用 if else 语句提示用户点击按钮以继续该语句? - How can I make an if else statement in JavaScript prompt a user to hit a button to continue the statement?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM