简体   繁体   English

单击div时如何切换

[英]How to toggle when a div has been clicked

Hello guys I have following code snippet 大家好,我有以下代码片段

<script>
$(document).ready(function(){
$(".ui-radio").click(function(){
   $("#textarea").toggle();
   return false;
});
});
</script> 

Then I have this HTML code where I am using mobile Jquery 然后我在使用移动Jquery的地方有此HTML代码

<fieldset data-role="controlgroup">
<input class="info" id="radio-choice-1" name="opcmessage" title="<?php echo $this->translate->translate("opcsend1","nucleo","En la propiedad, favor de enviarme más información")?>" type="radio" value="1" <?php if($opcmessage==1){?> checked="checked" <?php }?> />
<label for="radio-choice-1">&nbsp;En la propiedad, favor de enviarme más información</label>

<input class="info" id="radio-choice-2" name="opcmessage" title="<?php echo $this->translate->translate("opcsend2","nucleo","En la propiedad, deseo visitarla")?>" type="radio" value="2" <?php if($opcmessage==2){?> checked="" <?php }?> />
<label for="radio-choice-2">En la propiedad, deseo visitarla</label>
<?php if(count($images)<=0){?>
<input class="info" id="radio-choice-3" name="opcmessage" title="<?php echo $this->translate->translate("opcsend3","nucleo","En la propiedad, envieme fotos")?>" type="radio" value="3" <?php if($opcmessage==3){?> checked="" <?php }?> />
<label for="radio-choice-3">En la propiedad, envieme fotos</label>

<?php }else{?>

<input class="info" id="radio-choice-4" name="opcmessage" title="<?php echo $this->translate->translate("opcsend4","nucleo","En la propiedad, envieme más fotos")?>" type="radio" value="4" <?php if($opcmessage==4){?> checked="" <?php }?> />
<label for="radio-choice-4">En la propiedad, envieme más fotos</label>
</fieldset >

And I have this text area 我有这个文本区域

<div data-role="fieldcontain">
    <textarea cols="40" rows="8" name="textarea" id="textarea"></textarea>
</div>

I am trying to toggle the textarea on when one of the radio button is clicked . 我试图在单击单选按钮之一时打开文本区域。 I am able to toggle it in the normal browser but this is not working in the mobile device . 我可以在普通浏览器中切换它,但这在移动设备中不起作用。

Please help me out if you know why this problem is occurring ?? 如果您知道为什么会出现此问题,请帮助我?

Also I want to display the detail of the radio button in the text area .. Like if " En la propiedad, deseo visitarla" is slected the textarea should show that content inside it 我也想在文本区域中显示单选按钮的详细信息。就像选择“ En la propiedad,deseo visitarla”一样,文本区域应在其中显示该内容

EDIT 1 编辑1

Works on a normal DIV with out the Mobile JQuery tested . 在未经测试的Mobile JQuery的情况下,可在普通DIV上工作。 I think the problem is maybe the class name which mobile JQuery throws on browsers .... I am very new to this maybe I am wrong Thanks in Advance 我认为问题可能是移动JQuery在浏览器上抛出的类名称....我对此很陌生,也许我错了

EDIT 2 编辑2

Added the event 添加了活动

$('.ui-radio').bind('touchend',function(e){ $("#textarea").toggle();

It works fine but the problem is I only want to toggle hide and unhide when the first option is clicked . 它工作正常,但问题是我只想在单击第一个选项时切换隐藏和取消隐藏。 right now it toggles on all the options like even it is open . 现在它可以像打开一样打开所有选项。 When you click any other option it closes . 当您单击任何其他选项时,它将关闭。 Here is the script which I am using 这是我正在使用的脚本

    <script>
    $(document).ready(function(){
    $("#textarea").hide();
    $('.ui-radio').bind('touchend',function(e){
        $("#textarea").toggle();
       return false;
    });
    });

</script>

Edit 3 编辑3

<script>
$(document).ready(function(){
$("#textarea").hide();
$('.ui-radio').bind('touchstart',function(e){
    $("#textarea").show();
   return false;
});
});
</script> 

Problem is with the touch behavior of the div ui-radio . 问题在于div ui无线电的触摸行为。 If i try to touch that part and scroll down then it does not work properly . 如果我尝试触摸该部分并向下滚动,则它将无法正常工作。 Tested in Chrome, Opera and Firefox. 在Chrome,Opera和Firefox中进行了测试。

Edit 4 编辑4

Also I am trying to append some content int the #textarea bbut I am not able to do it can someone help me out with this . 我也试图在#textarea附加一些内容,但是我无法做到这一点,有人可以帮助我解决这个问题。

Script 脚本

<script>
$(document).ready(function(){
$("#textarea").hide();

$('.ui-radio').bind('touchstart',function(e){
    $('<p>En la propiedad, favor de enviarme más información</p>').appendTo('#textarea');
    $("#textarea").show();
  return false;
});

});
</script>

EDIT 5 编辑5

<script>
$(document).ready(function(){
$("#textarea").hide();

$('.ui-radio').bind('touchstart',function(e){
    $("#textarea").show();
    var a = $( this ).find( "label" ).attr('for');
    if(a == "radio-choice-1"){
     $("#textarea").html( "Estoy interesado(a) en la propiedad, favor de enviarme más información" );}
     if(a == "radio-choice-2"){
     $("#textarea").html( "Estoy interesado(a) en la propiedad, deseo visitarla" );}
     if(a == "radio-choice-3"){
     $("#textarea").html( "Estoy interesado(a) en la propiedad, envieme fotos" );}
     if(a == "radio-choice-4"){
     $("#textarea").html( "Estoy interesado(a) en la propiedad, envieme más fotos" );}
   return false;
});

});
</script> 

This is the script which I came up with it works fine but as I said the touch behavior is effectig other effects of JQuery and Mobile JQuery in the page . 这是我想出的脚本,它可以正常工作,但是正如我所说的,触摸行为会影响页面中JQuery和Mobile JQuery的其他效果。 Please suggest me something what I should do 请给我建议我该怎么做

EDIT 6 编辑6

here is a fiddle of it http://jsfiddle.net/7GY8K/ 这是它的小提琴http://jsfiddle.net/7GY8K/

my problem is that if you click on the options of the radio button it should open the textarea but its not happening. 我的问题是,如果单击单选按钮的选项,则应打开文本区域,但不会发生。 But if the same code I add in normal page which is not a hyperlink it works fine please have a look 但是,如果我在普通页面中添加的相同代码(不是超链接)可以正常工作,请看一下

To fire the event with mobile device you may try replacing 要使用移动设备触发事件,您可以尝试更换

$(".ui-radio").click(function()

with

$(".ui-radio").on('click touch', function ()

Hope it helps. 希望能帮助到你。

You should bind to touchstart or touchend events on mobile. 您应该绑定到移动设备上的touchstart或touchend事件。 Try this: 尝试这个:

$('.ui-radio').bind('touchstart',function(e){
    $("#textarea").toggle();
});
$(document).ready(function(){
   $(".slidingDiv").hide();
   $(".show_hide").show();

    $('.show_hide').click(function(){
    $(".slidingDiv").slideToggle();
  });
});

    <p>Toggle hide show/p>
    <div class="toggleMe">Toggle between hide() and show()</div>
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $(".click_me").click(function(){
    $(".hideshow").toggle();
  });
});
</script>
</head>
<body>
<div class="click_me">click me</div>
<br>
<br>
<div class="hideshow">This is a paragraph.</div>
</body>
</html>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM