【问题标题】:Provide content for a div with different ID name为具有不同 ID 名称的 div 提供内容
【发布时间】:2018-01-28 00:05:00
【问题描述】:

我正在尝试创建一个带有允许客户端复制表单的添加按钮的网站。在这样做的同时,我失去了由于重复而存在的隐藏/显示选项功能。 (ifYesifNo div,应该根据客户选择显示)

我的问题在于我的 JavaScript 中的变量“Valu”,或者我认为是这样。

下面是我的代码的 sn-p:

HTML:

<select id="ValuType" name="ValuType" onchange="priceAndCheck()">
    <option disabled="disabled" selected="selected">Type of Property</option>
    <option id="Apartment" value="Apartment">Apartment</option>
    <option id="Building" value="Building">Building</option>
    <option id="Farm" value="Farm">Farm</option>
    <option id="House" value="House">House</option>
    <option id="Land" value="Land">Land</option>
</select>


<div id="ifYes" class="ifYes">
...ifYes content comes here...
</div>

<div id="ifNo" class="ifNo">
...ifNo content comes here...
</div>

JavaScript:

    function yesnoCheck() {

    var Valu = document.getElementById("ValuType").value;

    if (Valu == 'Apartment' || Valu == 'Farm' || Valu == 'Land') {
        document.getElementById('ifYes').style.height = '250px';
        document.getElementById('ifYes').style.display = 'block';
        document.getElementById('ifYes').style.transition = 'all 1s ease 0.59s';
        document.getElementById('ifNo').style.height = '0px';
        document.getElementById('ifNo').style.overflow = 'hidden';
        document.getElementById('ifNo').style.transition = '0.55s';
    }
    else if (Valu == 'Building' || Valu == 'House') {
        document.getElementById('ifNo').style.height = '250px';
        document.getElementById('ifNo').style.display = 'block';
        document.getElementById('ifNo').style.transition = 'all 1s ease 0.59s';
        document.getElementById('ifYes').style.height = '0px';
        document.getElementById('ifYes').style.overflow = 'hidden';
        document.getElementById('ifYes').style.transition = '0.55s';
    }
    else {
        document.getElementById('ifYes').style.overflow = 'hidden';
        document.getElementById('ifNo').style.overflow = 'hidden';
    }
}



function providePrice() {

var a = document.getElementById("Region").value;
var b = document.getElementById("ValuReq").value;
var c = document.getElementById("ValuType").value;
var d = document.getElementById("Express").value;
var e = 25;




if (a=="Region1" && b=="Bank" && c=="Apartment" && d=="Yes")
{
    var x = 200 + e;
    document.getElementById("price").innerHTML = "Price: OMR " + x;

}

else if (a=="Region1" && b=="Bank" && c=="Apartment" && d=="No")
{
    var x = 200;
    document.getElementById("price").innerHTML = "Price: OMR " + x;
}

else if (a=="Region1" && b=="Bank" && c=="Building" && d=="Yes")
{
    var x = 350 + e;
    document.getElementById("price").innerHTML = "Price: OMR " + x;
}
...
...
...
else
{
    document.getElementById("price").innerHTML = "Price:"
}


    function getValue() {
if (document.getElementById("Express").checked) {
    document.getElementById("Express").value = "Yes";
 } else {
    document.getElementById("Express").value = "No";
 }
}


function priceAndCheck(){
    yesnoCheck();
    providePrice();
}

代码说明:
每当客户填写表格并选择某些选项时,它应该允许显示 div ifYesifNo 并填充价格。 (价格与多个字段相关,)我正在使用div 名称进行此操作,并且效果很好。但是,在克隆表单时,我无法为所有新克隆的表单生成 div ifYesifNo

我认为问题出在以下几点:

  • 我的 div 命名是不变的(在所有克隆形式中完全相同),这是问题之一
  • 调用 priceAndCheck() 函数调用两个函数 yesnoCheck()providePrice() 如果我开始调用一个元素,我需要更改所有函数(例如 function test(selectElement) -- 尝试过 [Sam Apostel 的解决方案][1] 和无法使用我所有可用的编码查看所需的选项。
  • 克隆的div 具有不同的id 名称,但其中的所有其他divs 具有相同的名称

如果您能就处理此问题的最佳方式提供帮助,我们将不胜感激。

【问题讨论】:

  • 您在何时何地致电function yesnoCheck()
  • @irshadjm 抱歉,我忘记在更改选项时调用了两个函数。 onchange="priceAndCheck()" 里面有yesnoCheck()函数。
  • 您正面临问题,因为一旦您的表单被复制,您将失去使用 id 获取对象的功能,因为现在您有多个具有相同 id 的元素,这是不允许的,在这种情况下,您将始终如果您尝试通过 ID 访问最后一个元素,我建议您更新逻辑以按类获取元素。
  • @amitwadhwani 当我使用上面的代码时,只有第一个元素具有功能。我将所有元素更改为通过 ClassName 接收,但它不起作用。我可能错过了一些东西,但我认为我的问题是我需要更改 ID 和类名的命名,对吗?
  • 与其依赖 DOM,您能否将这些元素的列表保存在一个数组(/dictionary,以 ID 作为键)或其他东西(在创建和添加元素时增加数组到 DOM)?

标签: javascript html css forms clone


【解决方案1】:

我认为这将解决您的问题(在onchange=""tag 中使用this

$ = function(id) {
  return document.getElementById(id).style;
}
function priceAndCheck(selectElement){

yesnoCheck(selectElement);
}
function yesnoCheck(selectElement) {
  var show;
  var hide;
  var Valu = selectElement.value;
  if (Valu == 'Apartment' || Valu == 'Farm' || Valu == 'Land') {
    show = $('ifYes');
    hide = $('ifNo');
  } else if (Valu == 'Building' || Valu == 'House') {
    show = $('ifNo');
    hide = $('ifYes');
  }
  show.height = '250px';
  show.display = 'block';
  show.transition = 'all 1s ease 0.59s';
  hide.height = '0px';
  hide.overflow = 'hidden';
  hide.transition = '0.55s';
}
<select id="form1" name="ValuType" onchange="priceAndCheck(this)">
    <option disabled="disabled" selected="selected">Type of Property</option>
    <option id="Apartment" value="Apartment">Apartment</option>
    <option id="Building" value="Building">Building</option>
    <option id="Farm" value="Farm">Farm</option>
    <option id="House" value="House">House</option>
    <option id="Land" value="Land">Land</option>
</select>


<div id="ifYes" class="ifYes">
  ...if Yes content comes here...
</div>

<div id="ifNo" class="ifNo">
  ...if No content comes here...
</div>

【讨论】:

  • 无法让它与我的代码一起使用,因为在更改选项时我有 2 个函数同时运行。我可以不只是在我正在调用的函数中添加this.id 吗?如: priceAndCheck() { yesnoCheck(this.id);函数2(); }
  • 我更改了答案以适应嵌套函数并将this.id 更改为this,这样您就不必查看DOM 来查找元素
  • 还是不行。如果我调用两个函数,我似乎不能使用这个方法,除非我也改变我的第二个函数,或者我假设。
  • 添加了更多代码和解释,希望对我的代码提供更多说明。
【解决方案2】:

经过多次测试和代码编辑,我想出了下面的查询解决方案,其中我利用每个克隆表单的类名,选项变得可视,根据需要。

function yesnoCheck() {
		
		var Valu ='';
		var count = 0;
	
		$('.ValuType').each(function() {
			
			Valu = $(this).attr('id');
			Valu = document.getElementById(Valu).value;
			
			

		    if (Valu == 'Apartment' || Valu == 'Farm' || Valu == 'Land') {
				document.getElementsByClassName('Yes')[count].style.height = '30px';
		        document.getElementsByClassName('Yes')[count].style.display = 'block';
				document.getElementsByClassName('Yes')[count].style.transition = 'all 1s ease 0.59s';
				document.getElementsByClassName('No')[count].style.height = '0px';
				document.getElementsByClassName('No')[count].style.overflow = 'hidden';
				document.getElementsByClassName('No')[count].style.transition = '0.55s';
		    }
			else if (Valu == 'Building' || Valu == 'House') {
				document.getElementsByClassName('No')[count].style.height = '30px';
				document.getElementsByClassName('No')[count].style.display = 'block';
				document.getElementsByClassName('No')[count].style.transition = 'all 1s ease 0.59s';
				document.getElementsByClassName('Yes')[count].style.height = '0px';
				document.getElementsByClassName('Yes')[count].style.overflow = 'hidden';
				document.getElementsByClassName('Yes')[count].style.transition = '0.55s';
			}
		    else {
				document.getElementsByClassName('Yes')[count].style.overflow = 'hidden';
				document.getElementsByClassName('No')[count].style.overflow = 'hidden';
			}
			
			
			count++;
		});

	}
  
  
  
  //define template
var template = $('#content:first').clone();


//define counter
var count = 0;

//add new section
$('body').on('click', '#repeat', function() {

	var clone = template.clone();
    //increment
    count++;
	
	//remove cross button div
	clone.find('cross').removeAttr("#cross");
	
	//update id of ifYes & ifNo
	clone.find('.Yes').prop('id', 'ifYes' + count);
	clone.find('.No').prop('id', 'ifNo' + count);
	
    //loop through each input
    var inputSection = clone.find(':input').each(function(){

        //set id to store the updated section number
        var newId = this.id + count;

        //update id
        this.id = newId;

    }).end()
	
    //inject new section with animation
    .appendTo('#content').hide()
	.appendTo('#content').slideDown(500)
    return false;
});

//remove section
$('#content').on('click', '.cross', function() {
    //slide up section
    $(this).parent().slideUp(500, function(){
        //remove parent element (main section)
        $(this).parent().child().empty();
        return false;
    });
    return false;
});
.Yes{
     overflow: hidden;
     height: 0;
     -webkit-transition: all 1.5s ease;
     -moz-transition: all 1.5s ease;
     -ms-transition: all 1.5s ease;
     -o-transition: all 1.5s ease;
     transition: all 1.5s ease;
}


.No{
    overflow: hidden;
    height: 0;
    -webkit-transition: all 1.5s ease;
    -moz-transition: all 1.5s ease;
    -ms-transition: all 1.5s ease;
    -o-transition: all 1.5s ease;
    transition: all 1.5s ease;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="content">
  <button type="button" id="cross" class="buttonImgTop cross">remove</button>
   <div id="ValuWrapper">
   
   <select id="ValuType" name="ValuType" class="ValuType" onchange="yesnoCheck()">
	<option disabled="disabled" selected="selected">Type of Property</option>
	<option id="Apartment" value="Apartment">Apartment</option>
	<option id="Building" value="Building">Building</option>
	<option id="Farm" value="Farm">Farm</option>
	<option id="House" value="House">House</option>
	<option id="Land" value="Land">Land</option>
   </select>
                   
      <div id="ifYes" class="Yes"> 'Yes' class text comes here </div>
      <div id="ifNo" class="No">  'No' class text comes here </div>
   </div>
</div>

<button type="button" class="buttonImg" id="repeat">repeat</button>

【讨论】:

    猜你喜欢
    • 2016-03-18
    • 1970-01-01
    • 2021-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 2020-03-19
    相关资源
    最近更新 更多