gpt4 book ai didi

Simple HTML update of a variable based on the value of another(根据一个变量的值对另一个变量进行简单的HTML更新)

转载 作者:bug小助手 更新时间:2023-10-25 17:48:35 24 4
gpt4 key购买 nike



I have some code on my website that updates an element based on the value of a price slider.

我在我的网站上有一些代码,根据价格滑块的值更新元素。


$("#te_li_yr_seats_html").html(te_li_yr_price.innerHTML/215)

The above code is working fine, and #te_li_yr_seats_html will always be a number between 2 and 8.

上面的代码工作正常,#te_li_yr_seats_html将始终是2到8之间的数字。


I want to update a second element based on this calculated value.

我想要基于这个计算值更新第二个元素。


I've added my full code below and then a pseudo code snippet of what I'm trying to do. I hope that's clear.

我在下面添加了完整的代码,然后添加了我想要做的事情的伪代码片段。我希望你说得很清楚。


Thank you in advance for any help.

事先感谢您的帮助。


var te_li_yr_price = document.getElementById("te_li_yr_price_base");
var te_li_yr_slider = document.getElementById("te_li_yr_slider");

te_li_yr_price.innerHTML = te_li_yr_slider.value;

te_li_yr_slider.oninput = function() {
te_li_yr_price.innerHTML = this.value;

$("#te_li_yr_price_html").html(te_li_yr_price.innerHTML.replace(/\B(?=(\d{3})+(?!\d))/g, ","))
$('input[id="te_li_yr_price_form"]').val(te_li_yr_price.innerHTML)

$("#te_li_yr_seats_html").html(te_li_yr_price.innerHTML/215)
$('input[id="te_li_yr_seats_form"]').val(te_li_yr_price.innerHTML/215)


/* PSEUDO CODE BELOW */

if $("#te_li_yr_seats_html").html = 2 then
my_new_variable = 70

elseif $("#te_li_yr_seats_html").html = 3 then
my_new_variable = 100

elseif $("#te_li_yr_seats_html").html = 4 then
my_new_variable = 130

elseif $("#te_li_yr_seats_html").html = 5 then
my_new_variable = 160

elseif $("#te_li_yr_seats_html").html = 6 then
my_new_variable = 190

elseif $("#te_li_yr_seats_html").html = 7 then
my_new_variable = 220

elseif $("#te_li_yr_seats_html").html = 8 then
my_new_variable = 250

/* then apply to HTML */
$("#my_new_variable_html").html(my_new_variable)

更多回答

To get the HTML value, use $("..").html(). And to compare values use === (or just == if comparing "2" with 2 - .html() will return a string. In this case, just create a variable: var seats = te_li_yr_price.innerHTML/215 then $("#te_li_yr_seats_html").html(seats) and if (seats === 2) etc (or use a switch)

要获取HTML值,请使用$(“..”).html()。如果将“2”与2-.html()进行比较将返回一个字符串,则使用=(或仅使用==)来比较值。在本例中,只需创建一个变量:var Seats=te_li_yr_price.innerHTML/215 Then$(“#te_li_yr_Seats_html”).html(Seats)和if(Seats=2)等(或者使用开关)

my_new_var = seats * 30 + 10?

My_new_var=座位*30+10?

Thank you for both of these comments I think I have it working now.

谢谢你的这两条评论,我想我现在已经成功了。

优秀答案推荐

The whole thing is hard to grasp, but here is a version that is easier to change. Note I have converted all the script to jQuery instead of mixing it with plain JS DOM access

整件事很难理解,但这里有一个更容易改变的版本。注意,我已经将所有脚本转换为jQuery,而不是将其与普通的JSDOM访问混合在一起


I am using a ternary for the lookup to make sure that you do not get an error if the perSeat is larger than the lookup table

我使用三元组进行查找,以确保如果perSeat大于查找表,则不会出现错误


const $te_li_yr_price = $("#te_li_yr_price_base");
const $te_li_yr_slider = $("#te_li_yr_slider");
const $te_li_yr_price_html = $("#te_li_yr_price_html");
const $te_li_yr_price_form = $("#te_li_yr_price_form");
const $te_li_yr_seats_html = $("#te_li_yr_seats_html");
const $te_li_yr_seats_form = $("#te_li_yr_seats_form");
const $my_new_variable_html = $("#my_new_variable_html");

const lookup = [0, 0, 70, 100, 130, 160, 190, 220, 250];

$te_li_yr_slider.on("input", function() {
let val = this.value; // string
let formattedVal = val.replace(/\B(?=(\d{3})+(?!\d))/g, ","); // now with thousand separators
let perSeat = parseInt(val / 215);
$te_li_yr_price.html(val);
$te_li_yr_price_html.html(formattedVal);
$te_li_yr_price_form.val(te_li_yr_priceval);
$te_li_yr_seats_html.html(perSeat);
$te_li_yr_seats_form.val(perSeat);
// if perSeat is <2 or outside the lookup table, give default value
$my_new_variable_html.html(
perSeat < 2 || perSeat >= lookup.length ? val : lookup[perSeat]
);
}).trigger("input"); // initialise


更多回答

Thank you appreciate this. I think I have found a solution based on the first comment received which is more to my basic level ;)

谢谢你,感谢你这么做。我认为我已经根据收到的第一条评论找到了解决方案,这更符合我的基本水平。)

I strongly recommend you look and try to understand what I coded since it will help you in the long run

我强烈建议您查看并尝试理解我编写的代码,因为从长远来看,它会对您有所帮助

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